public class CanJump {

    public static Boolean answer(int[] nums){
        if(nums[0]==0){
            return Boolean.FALSE;
        }
        Boolean a= getaBoolean(nums, 0);
        if(a){
            return a;
        }
        Boolean b= getaBoolean(nums, 1);
        return b;
    }

    private static Boolean getaBoolean(int[] nums, int p2) {
        int max= nums.length-1;
        if(nums.length==1){
            return Boolean.TRUE;
        }
        while (p2 <max){
            p2 += nums[p2];
            if(p2>=max){
                return Boolean.TRUE;
            }
            if(nums[p2]==0){
                return Boolean.FALSE;
            }
        }
        return Boolean.TRUE;
    }


    public static Integer answer2 (int[] nums){
        int max=0;
        int end=0;
        int step=0;
        for (int i = 0; i < nums.length - 1; i++) {
            max=Math.max(max,i+nums[i]);
            if(i==end){
                end=max;
                step++;
            }
        }
        return step;
    }
    public static void main(String[] args) {
        int[] nums=new int[]{2,3,0,1,4};
        System.out.println(answer2(nums));
    }
}
